package org.JQbitorrentClinet.impl.V283;

import org.JQbitorrentClinet.QBitorrentConnectionInfo;
import org.JQbitorrentClinet.base.BaseProxy;
import org.JQbitorrentClinet.interfaces.SearchProxy;
import org.JQbitorrentClinet.pojo.search.*;
import org.JQbitorrentClinet.request.search.*;

import java.util.List;

public class SearchProxy_V2_8_3 extends BaseProxy implements SearchProxy {

    public SearchProxy_V2_8_3(QBitorrentConnectionInfo connectionInfo) {
        super(connectionInfo);
    }

    @Override
    public SearchJob startSearch(String pattern, List<String> plugins) {
        login();
        String[] pluginArray = plugins.toArray(new String[0]);
        StartSearchParam startSearchParam = StartSearchParam.builder()
                .pattern(pattern)
                .plugins(pluginArray)
                .category("all")
                .build();
        return process(startSearchParam);
    }

    @Override
    public SearchJob startSearch(String pattern) {
        login();
        StartSearchParam startSearchParam = StartSearchParam.builder()
                .pattern(pattern)
                .category("all")
                .plugins(new String[]{"all"})
                .build();
        return process(startSearchParam);
    }

    @Override
    public SearchJob startSearch(String pattern, String category) {
        login();
        StartSearchParam startSearchParam = StartSearchParam.builder()
                .pattern(pattern)
                .category(category)
                .plugins(new String[]{"all"})
                .build();
        return process(startSearchParam);
    }

    @Override
    public SearchJob startSearch(String pattern, List<String> plugins, String category) {
        login();
        String[] pluginArray = plugins.toArray(new String[0]);
        StartSearchParam startSearchParam = StartSearchParam.builder()
                .pattern(pattern)
                .plugins(pluginArray)
                .category(category)
                .build();
        return process(startSearchParam);
    }

    @Override
    public boolean stopSearch(Integer id) {
        login();
        StopSearchParam stopSearchParam = StopSearchParam.builder()
                .id(id)
                .build();
        process(stopSearchParam);
        return true;
    }

    @Override
    public SearchJobResult getSearchResult(Integer id) {
        login();
        GetSearchResultParam getSearchResultParam = GetSearchResultParam.builder()
                .id(id)
                .build();
        SearchJobResult searchJobResults = process(getSearchResultParam);
        return searchJobResults;
    }

    @Override
    public SearchJobStatus getSearchStatus(Integer id) {
        login();
        GetSearchStatusParam getSearchStatusParam = GetSearchStatusParam.builder()
                .id(id)
                .build();
        List<SearchJobStatus> searchJobStatuses = process(getSearchStatusParam);
        return searchJobStatuses.get(0);
    }

    @Override
    public List<SearchJobStatus> getSearchStatuses() {
        login();
        GetSearchStatusParam getSearchStatusParam = GetSearchStatusParam.builder()
                .build();
        List status = process(getSearchStatusParam);
        if(status == null)
            status = List.of();
        return status;
    }

    @Override
    public boolean deleteSearch(Integer id) {
        login();
        DeleteSearchParam deleteSearchParam = DeleteSearchParam.builder()
                .id(id)
                .build();
        process(deleteSearchParam);
        return true;
    }

    @Override
    public List<SearchPlugin> getSearchPlugins() {
        login();
        GetSearchPluginsParam getSearchPluginsParam = GetSearchPluginsParam.builder()
                .build();
        List plugins = process(getSearchPluginsParam);
        if(plugins == null)
            plugins = List.of();
        return plugins;
    }

    @Override
    public boolean installSearchPlugin(String url) {
        login();
        String[] urls = {url};
        InstallSearchPluginsParam installSearchPluginParam = InstallSearchPluginsParam.builder()
                .sources(urls)
                .build();
        process(installSearchPluginParam);
        return true;
    }

    @Override
    public boolean installSearchPlugins(List<String> urls) {
        login();
        String[] urlArray = urls.toArray(new String[0]);
        InstallSearchPluginsParam installSearchPluginParam = InstallSearchPluginsParam.builder()
                .sources(urlArray)
                .build();
        process(installSearchPluginParam);
        return true;
    }

    @Override
    public boolean removeSearchPlugins(List<String> names) {
        login();
        String[] nameArray = names.toArray(new String[0]);
        UninstallSearchPluginsParam uninstallSearchPluginsParam = UninstallSearchPluginsParam.builder()
                .names(nameArray)
                .build();
        process(uninstallSearchPluginsParam);
        return true;
    }

    @Override
    public boolean removeSearchPlugin(String name) {
        login();
        String[] names = {name};
        UninstallSearchPluginsParam uninstallSearchPluginsParam = UninstallSearchPluginsParam.builder()
                .names(names)
                .build();
        process(uninstallSearchPluginsParam);
        return true;
    }

    @Override
    public boolean enableSearchPlugins(List<String> names, boolean enabled) {
        login();
        String[] nameArray = names.toArray(new String[0]);
        EnableSearchPluginsParam enableSearchPluginsParam = EnableSearchPluginsParam.builder()
                .names(nameArray)
                .enable(enabled)
                .build();
        process(enableSearchPluginsParam);
        return true;
    }

    @Override
    public boolean enableSearchPlugin(String name, boolean enabled) {
        login();
        String[] names = {name};
        EnableSearchPluginsParam enableSearchPluginsParam = EnableSearchPluginsParam.builder()
                .names(names)
                .enable(enabled)
                .build();
        process(enableSearchPluginsParam);
        return true;
    }

    @Override
    public boolean updateSearchPlugins() {
        login();
        UpdateSearchPluginsParam updateSearchPluginsParam = UpdateSearchPluginsParam.builder()
                .build();
        process(updateSearchPluginsParam);
        return true;
    }
}
